const tui = {
	toast: function(text, duration, success, isShow) {
		uni.showToast({
			title: text,
			icon: success ? 'success' : 'none',
			duration: duration || 2000
		})
	},
	interfaceUrl: function() {
		//接口地址
		// return "" // 线上
		// return "" // 测试
		return "http://47.106.181.180:8088" //本地
	},
	uploadUrl: function() {
		return  `${this.interfaceUrl()}/file/upload/oss`
	},
	request: function(url, postData = {}, method = 'get', showLoading = false, isShow = true, header = {}) {
		//接口请求
		if (showLoading) {
			uni.showLoading({
				mask: true,
				title: '请稍候...'
			})
		}
		postData.forntNowTime = new Date().getTime()
		return new Promise((resolve, reject) => {
					console.log(postData, url)
			uni.request({
				url: this.interfaceUrl() + url,
				data: postData,
				header: {
					'Content-Type': 'application/x-www-form-urlencoded',
					'token': this.getToken(),
					... header
				},
				method: method, //'GET','POST'
				dataType: 'json',
				success: (res) => {
					console.log(res)
					showLoading && uni.hideLoading()
					if (res.data && res.data.msg) {
						res.data.msg = res.data.msg.replace(/<br\s*\/?>/gi,"\n");
						console.log(res.data.msg)
					}
					if (res.data.code === -1) {
						uni.showToast({
							icon: 'none',
							title: '登录过期,请重新登录',
							duration: 3000
						})
						uni.removeStorageSync('fw_token')
						uni.reLaunch({
							url: '/pages/user/index'
						})
					}
					if (!res.data.success && isShow) {
						uni.showToast({
							icon: 'none',
							title: res.data.msg
						})
					}
					resolve(res.data)
				},
				fail: (res) => {
					console.log(res)
					uni.showToast({
						icon: 'none',
						title: '请求失败,请稍后重试'
					})
					showLoading && uni.hideLoading()
					reject(res)
				}
			})
		})
	},
	uploadFile: function(url, imgPath, postData, fileName) {
		const that = this
		uni.showLoading({
			title: '请稍候...'
		})
		return new Promise((resolve, reject) => {
			const uploadTask = uni.uploadFile({
				url: this.interfaceUrl() + url,
				filePath: imgPath,
				name: fileName ? fileName : 'avatar',
				header: {
					// 'content-type': 'multipart/form-data',
					'token': this.getToken() ? this.getToken() : ''
				},
				formData: postData,
				success: function(res) {
					uni.hideLoading()
					res.data = JSON.parse(res.data)
					if (res.data.code !== 0) {
						that.toast(res.data.msg)
					}
					resolve(res.data)
				},
				fail: function(res) {
					reject(res)
					uni.hideLoading();
					that.toast(res.message);
				}
			})
		})
	},
	setToken: function(token, userType) {
		uni.setStorageSync((userType === 1?"fw_token":"provider_token"), token)
	},
	getToken() {
		const userType = this.getUserType()
		if (userType == 1) {
			return uni.getStorageSync("fw_token")
		} else if (userType == 2) {
			return uni.getStorageSync("provider_token")
		} else {
			return ''
		}
	},
	getUserType() {
		return uni.getStorageSync("fw_userType")
	},
	setUserType(userType) {
		uni.setStorageSync("fw_userType", userType)
	},
	isLogin: function() {
		return uni.getStorageSync("fw_token") ? true : false
	}
}

export default tui
